package com.lh.mvc.spitter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.lh.mvc.base.BaseDao;

public class SpitterIbatisDao implements BaseDao{
	private static final Logger logger = Logger.getLogger(SpitterService.class);
	private List<Map<String, Object>> resultList = null;
	private Map<String, Object> prepareMap = null;
	private Map<String, Object> resultMap = null;
	private SqlMapClientTemplate sqlMapClientTemplate;
	
	public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
		this.sqlMapClientTemplate = sqlMapClientTemplate;
	}

	public List<Map<String, Object>> searchPersons() {
		System.out.println("ibatis SpitterIbatisDao.searchPersons");
		List<Map<String, Object>> resultList = sqlMapClientTemplate.queryForList("spitter.getPersons");
		System.out.println("spitter.getPersons==>"+resultList);
		return resultList;
	}

	@SuppressWarnings("deprecation")
	public int addUser(Person person) {
		int result = 0;
		System.out.println("SpitterIbatisDao.addUser======>"+person);
		
		prepareMap = new HashMap<String, Object>();
		prepareMap.put("name", person.getName());
		prepareMap.put("age", Integer.parseInt(person.getAge()));
		prepareMap.put("sex", person.getSex());
		prepareMap.put("city", person.getCity());

		logger.info("spitter.addPerson==prepareMap===>"+prepareMap);
		
		result = (Integer)sqlMapClientTemplate.insert("spitter.addPerson", prepareMap);
		
		System.out.println("insert result======>"+result);
		
		return result;
	}

	public Map<String, Object> getUser(String id) {
		logger.info("SpitterIbatisDao.addUser===id===>" + id);

		resultMap = (Map<String, Object>)sqlMapClientTemplate.queryForObject("spitter.getPerson", Integer.parseInt(id));

		logger.info("SpitterIbatisDao.addUser===resultMap===>" + resultMap);
		
		return resultMap;
	}

	public int updateUser(Person person) {
		int result = 0;
		logger.info("SpitterIbatisDao.updateUser======>"+person);
		
		prepareMap = new HashMap<String, Object>();
		prepareMap.put("id", Integer.parseInt(person.getId()));
		prepareMap.put("name", person.getName());
		prepareMap.put("age", Integer.parseInt(person.getAge()));
		prepareMap.put("sex", person.getSex());
		prepareMap.put("city", person.getCity());

		logger.info("spitter.updateUser==prepareMap===>"+prepareMap);
		
		result = (Integer)sqlMapClientTemplate.update("spitter.updatePerson", prepareMap);
		
		System.out.println("insert result======>"+result);
		
		return result;
	}

	public boolean deleteUsers(String[] ids) {
		logger.info("SpitterIbatisDao.deleteUsers======>");
		/*
		for (String id:ids) {
			logger.info("SpitterIbatisDao.deleteUsers======> id=" + id);
			sqlMapClientTemplate.delete("spitter.deletePersonById", Integer.parseInt(id));
		}
		*/
		List<String> paramList = new ArrayList<String>();
		paramList = Arrays.asList(ids);
		int delete = 0 ;
		if (ids != null && ids.length > 0) {
			delete = sqlMapClientTemplate.delete("spitter.deletePersons", paramList);
			logger.info("delete=>" + delete + "    " + paramList.toString());
		}
		
		return true;
	}

}
